//******创建XMLHttpRequest对象***
function createXMLHttp() {
	var XMLHttpFactories = [
	function() {
		return new XMLHttpRequest();
	},
	function() {
		return new ActiveXObject("Msxml12.XMLHTTP");
	},
	function() {
		return new ActiveXObject("Msxml13.XMLHTTP");
	},
	function() {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}];
	var xmlhttp = false;
	for (var i = 0; i < XMLHttpFactories.length; i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		} catch(e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

//*********
function request(url, callback, data) {
	var xmlHttp = createXMLHttp();
	if (!xmlHttp)
		return;
	var method = (data) ? "post" : "get";
	xmlHttp.open(method, url, true);
	xmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	if (data)
		xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState != 4)
			return;
		if (xmlHttp.status != 200 && xmlHttp.status != 304) {
			alert('HTTP' + xmlHttp.status);
			return;
		}
		callback(xmlHttp);
	}
	if (xmlHttp.readyState == 4)
		return;
	xmlHttp.send(data);
}

//**************事件监听
function listenEvent(eventTarget, eventType, eventHandler) {
	if (eventTarget.addEventListener) {
		eventTarget.addEventListener(eventType, eventHandler, false);
	} else if (eventTarget.attachEvent) {
		eventType = "on" + eventType;
		eventTarget.attachEvent(eventType, eventHandler);
	} else {
		eventTarget["on" + eventTpye] = eventHandler;
	}
}

//*******************停止事件监听
function stopListening(eventTarget, eventType, eventHandler) {
	if (eventTarget.removeEventListener) {
		eventTarget.removeEventListener(eventType, eventHandler, false);
		//IE9等其他浏览器
	} else if (eventTarget.attachEvent) {
		detachEvent = "on" + eventType;
		eventTarget.detachEvent(eventType, eventHandler);
		//IE 6,7,8
	} else {
		eventTarget["on" + eventType] = null;
	}
	//IE5
}

/** 取消事件*/
function cancelEvent(event) {
	if (event.preventDefault) {
		event.preventDefault();
		//如果可以取消事件的默认行为，则取消该行为
	} else {
		event.returnValue = false;
		//设置事件的返回值为false,即取消事件处理
	}
}

//******************阻止冒泡
function cancelPropagation(event) {
	event = event || window.event;
	if (event.stopPropagation) {
		event.stopPropagation();
	} else {
		event.cancelBubble = true;
	}
}

/*获取最大最小值*/
Array.min = function(array) {
	return Math.min.apply(Math, array);
}
Array.max = function(array) {
	return Math.max.apply(Math, array);
}
//求和
Array.prototype.sum = function() {
	for (var sum = i = 0; i < this.length; i++)
		sum += parseInt(this[i]);
	return sum
};
//求最大值
Array.prototype.maxnum = function() {
	for (var i = 0, maxValue = Number.MIN_VALUE; i < this.length; i++) {
		if (parseInt(this[i]) > maxValue) {
			maxValue = this[i];
		}
	}
	return maxValue
};

//球最小值
Array.prototype.minnum = function() {
	for (var i = 0, minValue = Number.MAX_VALUE; i < this.length; i++) {
		if (parseInt(this[i]) < minValue) {
			minValue = this[i];
		}
		return minValue;
	}
}
//应用
//var arr = [1,21,3,4,22,45,6,7,32];
//alert(arr.join("+") + "=" + arr.sum()); //和：141
//alert(arr.join("|") + "中, 最小的数是:" + arr.minnum()); //最小数：1
//alert(arr.join("|") + "中, 最大的数是:" + arr.maxnum()); //最大数：45

/********************
 * 取窗口滚动条高度
 ******************/
function getScrollTop() {
	var scrollTop = 0;
	/**IE6以及以上版本(除了谷歌浏览器)都不支持document.body.scrollTop*/
	if (document.documentElement.scrollTop) {
		scrollTop = document.documentElement.scrollTop;
	} else if (document.body.scrollTop) {
		/**谷歌浏览器 以及 IE6以下版本支持document.body.scrollTop*/
		scrollTop = document.body.scrollTop;
	}
	return scrollTop;
}

/********************
 * 取窗口可视范围的高度         同理宽度亦是如此
 *******************/

function getClientHeight() {
	var clientHeight = 0;
	/**在I6以及IE6以上版本 两种都支持
	 * 而document.body.clientHeight获取的是文档body高度(由于兼容性问题实际应用中用scrollHeight获取文档高度)而不是可视区域的高度**/
	if (document.body.clientHeight && document.documentElement.clientHeight) {
		var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
	} else {//IE6以下版本只支持document.body.clientHeight 即可视区域高度
		var clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
	}
	return clientHeight;
}

/********************
 * 取文档内容实际高度
 *******************/
function getScrollHeight() {
	/**document.body.scrollHeight,document.documentElement.scrollHeight
	 *所以浏览器中都支持这两种   IE6以及高级浏览器中用scrollHeight（高度值更大） IE6以下两者高度一样*/
	return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}

if (getScrollTop() + getClientHeight() >= getScrollHeight() - 400) {
	//return true;
}

/**
 *去掉字符串的空白 */
if ( typeof String.trim == 'undefined') {
	String.prototype.trim = function() {
		return this.replace(/(^\s*)|(\s*$)/g, "");
	}
}

/**根据样式获取元素*/
function getByClass(Oparent, myClass) {
	var eles = Oparent.getElementsByTagName('*');
	var arr = [];

	//  i为忽略大小写
	//var re=new RegExp('\\b'+myClass+'\\b','i');

	var re = new RegExp('\\b' + myClass + '\\b');

	//var re='/\\b'+myClass+'\\b/i';
	//re=eval(re);

	for (var i = 0; i < eles.length; i++) {
		if (re.test(eles[i].className)) {
			arr.push(eles[i]);
		}
	}
	return arr;
}

/**快速排序*/
//alert(quickSort([12,5,37,6,22,50]));
function quickSort(arr) {
	if (arr.length <= 1) {
		return arr;
	}
	var num = Math.floor(arr.length / 2);
	var numValue = arr.splice(num, 1);
	var left = [];
	var right = [];
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] < numValue) {
			left.push(arr[i]);
		} else {
			right.push(arr[i]);
		}
	}
	return quickSort(left).concat([numValue], quickSort(right));
}

/**冒泡排序*/
//alert(bubbleSort([12,5,37,6,22,50]));
function bubbleSort(arr) {
	var temp = 0;
	for (var i = 0; i < arr.length; i++) {
		for (var j = 0; j < arr.length - i; j++) {
			if (arr[j] > arr[j + 1]) {
				temp = arr[j + 1];
				arr[j + 1] = arr[j];
				arr[j] = temp;
			}
		}
	}
	return arr;
}